#include "HelloWorldScene.h"

Scene* Space::createScene()
{
	auto scene = Scene::create();
	auto layer = Space::create();
    scene->addChild(layer);
    // return the scene
    return scene;
}

void Space::changeScaleLayer(Sprite* sprite){
	auto changeScaleListener = EventListenerTouchAllAtOnce::create();
	changeScaleListener->onTouchesBegan = [&](const std::vector<Touch*>& touches, Event *unused_event){

	};
	changeScaleListener->onTouchesMoved = [&](const std::vector<Touch*>& touches, Event *unused_event){
		auto target = static_cast<Sprite*>(unused_event->getCurrentTarget());
		if (touches.size() > 1 ){

			Point Cpos1 =target->convertToNodeSpace(Director::getInstance()->convertToGL(touches[0]->getLocationInView()));
			Point Cpos2 =target->convertToNodeSpace(Director::getInstance()->convertToGL(touches[1]->getLocationInView()));
			Point Ppos1 =target->convertToNodeSpace(Director::getInstance()->convertToGL(touches[0]->getPreviousLocationInView())) ;
			Point Ppos2 =target->convertToNodeSpace(Director::getInstance()->convertToGL(touches[1]->getPreviousLocationInView())) ;

			float Cdis = Cpos1.getDistance(Cpos2);
			float Pdis = Ppos1.getDistance(Ppos2);

			float height = target->getContentSize().height;
			float width = target->getContentSize().width;

			Point preAnPoint = target->getAnchorPoint();

			target->setAnchorPoint(Point((Cpos1.x + Cpos2.x) / 2 / width, (Cpos1.y + Cpos2.y) / 2 / height));

			Point newPos = (target->getAnchorPoint() - preAnPoint);

			Cpos1 = (Director::getInstance()->convertToGL(touches[0]->getLocationInView()));
			Cpos2 = (Director::getInstance()->convertToGL(touches[1]->getLocationInView()));
			Ppos1 = (Director::getInstance()->convertToGL(touches[0]->getPreviousLocationInView()));
			Ppos2 = (Director::getInstance()->convertToGL(touches[1]->getPreviousLocationInView()));

			target->setPosition(Point(newPos.x*width, newPos.y*height)*target->getScale() + target->getPosition()+(Cpos1+Cpos2-Ppos1-Ppos2)/2);

			target->setScale(MIN(4,MAX(0.5,Cdis / Pdis*target->getScale())));
			
		}
		if (touches.size() == 1){
			Point Cpos = (Director::getInstance()->convertToGL(touches[0]->getLocationInView()));
			Point Ppos = (Director::getInstance()->convertToGL(touches[0]->getPreviousLocationInView()));;
			target->setPosition((target->getPosition()+(Cpos-Ppos)));
		}
	};

	changeScaleListener->onTouchesEnded = [&](const std::vector<Touch*>& touches, Event *unused_event){
		 
	};

	changeScaleListener->onTouchesCancelled = [&](const std::vector<Touch*>& touches, Event *unused_event){

	};

	sprite->getEventDispatcher()->addEventListenerWithSceneGraphPriority(changeScaleListener, sprite);

}


// on "init" you need to initialize your instance
bool Space::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }

// 	Air* air = Air::create();
// 	this->addChild(air, 1, 1);
// 	Sprite* star1;
// 
// 	for (int i = 0; i < 3; i++){
// 
// 		star1 = Sprite::create("star1.png");
// 
// 		star1->setScale(0.2);
// 
// 		star1->setPosition(Point(300, 100*i+100));
// 
// 		this->addChild(star1);
// 
// 		MoveBy* fly = MoveBy::create(10-i, Point(500, 0));
// 
// 		star1->runAction(fly);
// 		//star2->runAction(fly);
// 
// 		air->checkCrash(star1);
// 		//air->checkCrash(star2);
// 	}
	//this->setScale(0.3);
	SpriteFrameCache* frameCache1 = SpriteFrameCache::getInstance();
	frameCache1->addSpriteFramesWithFile("Normal_Planet.plist", "Normal_Planet.png");

	SpriteFrameCache* frameCache2 = SpriteFrameCache::getInstance();
	frameCache2->addSpriteFramesWithFile("Asteroid_Ordinary.plist", "Asteroid_Ordinary.png");


	Size visibleSize = Director::getInstance()->getVisibleSize();
	Point origin = Director::getInstance()->getVisibleOrigin();
	Meteorolite *m_star = Meteorolite::create();
	m_star->bindSprite(Sprite::create("star.png"));
	m_star->setScale(0.1);

	MeteManager* meteManager = MeteManager::create();

	meteManager->addChild(m_star, 0);
	Sprite* universe = Sprite::create("universe.jpg");
	this->addChild(universe);
	universe->addChild(meteManager);
	universe->setPosition(visibleSize.width / 2, visibleSize.height / 2);
	this->changeScaleLayer(universe);
	return true;
}